const {
    getArticleList
} = require('../../utils/request')
const {
    article
} = require('../../utils/config')
const {
    hitsCount
} = require('../../utils/cloudRequest')

Page({

    data: {
        //标签列表
        tabs: [{
            category: 0,
            label: '推荐'
        },
            {
                category: 1,
                label: '训练'
            },
            {
                category: 2,
                label: '瑜伽'
            },
            {
                category: 3,
                label: '跑步'
            }
        ],
        loading: null,
        //视频列表
        articleList: [],

        navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') + 'px',
    },

    //根据标签 请求相应类型的数据
    onTab(event) {
        const category = event.detail
        this.setData({
            loading: true
        })
        getArticleList(category).then(res => {
            this.setData({
                articleList: res.data,
                loading: false
            })
        })
    },

    // 跳转视频详情页面 
    detailHandle(event) {
        const {
            id
        } = event.currentTarget.dataset;
        // 调用云函数
        hitsCount(article, id).then()
        // 跳转
        wx.navigateTo({
            url: `../articleDetail/index?id=${id}`,
        })

    },

    //第一次默认获取 热门 类型的 视频数据
    getArticleList(category) {
        //首次请求默认热门视频列表
        this.setData({
            loading: true
        })
        getArticleList( category).then(res => {
            this.setData({
                articleList: res.data,
                loading: false
            })
        })
    },

    onReady() {
        this.getArticleList(0);
    },

    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                current: 0
            })

        }
    },
})